
package com.sisgr.modelo.servico;

import com.sisgr.modelo.Colaborador;
import java.util.List;
import javax.annotation.Resource;
import org.apache.log4j.Logger;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Component;

/**
 * @author Alessandro
 */
@Component
public class ColaboradorService {
    
    protected static Logger logger = Logger.getLogger("service");
	
	@Resource(name="sessionFactory")
	private SessionFactory sessionFactory;
	
	public List<Colaborador> getAll() {
		logger.debug("Retrieving all persons");
		Session session = sessionFactory.getCurrentSession();
		Query query = session.createQuery("FROM Colaborador");
		return  query.list();
	}
	
	public Colaborador get( Integer id ) {
                Session session = sessionFactory.getCurrentSession();
		Colaborador colaborador = (Colaborador) session.get(Colaborador.class, id);
		return colaborador;
	}
	
	public void add(Colaborador colaborador) {
		logger.debug("Adicionando novo colaborador");
		Session session = sessionFactory.getCurrentSession();
		session.save(colaborador);
	}
	
	public void delete(Integer id) {
		logger.debug("Deletando caso exista o colaborador");
		Session session = sessionFactory.getCurrentSession();
		Colaborador colaborador = (Colaborador) session.get(Colaborador.class, id);
		session.delete(colaborador);
	}
    
}
